package filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
import java.io.PrintWriter;

public class Filter implements javax.servlet.Filter {
    FilterConfig config = null;//定义一个FilterConfig对象为类的实例变量

    public void init(FilterConfig filterConfig) throws ServletException {
        config = filterConfig;//获取FilterConfig对象引用
    }

    public void doFilter(ServletRequest request, ServletResponse response,
                         FilterChain chain) throws IOException, ServletException {

        String str = config.getInitParameter("course");//获取过滤器初始参数

        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.println("<font color=blue>前置程序块<br>");
        out.println("过滤器初始参数:course=" + str + "</font><br><br>");

        chain.doFilter(request,response);//调用"过滤器链"方法

        out.println("<br><font color=blue>后置程序块</font><br>");
    }

    public void destroy() {//过滤器"销毁"方法
       this.config = null;
    }

}
